package com.ykt.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.ykt.common.utils.PageUtil;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;

/**
 * @author wing
 * @version 1.0.0
 */
public class UserCollect extends BaseModel<UserCollect> {
    private static final long serialVersionUID = 1L;

    public static final String user_id = "user_id";
    public static final String shop_id = "shop_id";

    public static final UserCollect dao = new UserCollect();


    /**
     * 根据user Id查询其所有收藏信息
     * @param param 用户ID
     * @return
     */
    public PageData<UserCollect> pageFindByUserId(PageParam<Integer> param){
        String select = "SELECT s.id shopId, s.name shopName, s.logo shopLogo, s.merchant_id merchantId, "
                + " s.summary shopSummary, u.phone merchantPhone, 1 collected ";
               // + " c.pic_url cameraPic, c.camera_id cameraId, c.device_serial deviceSerial ";

        String exceptSelect = " FROM shop s "
                + " left join user u on s.merchant_id=u.id "
               // + " left join camera_shop cs on cs.shop_id=s.id "
               // + " left join camera c on cs.camera_id=c.id "
                + " left join user_collect uc on uc.shop_id=s.id "
                + " WHERE s.status=1 and uc.user_id=? ";

        Page<UserCollect> page = this.paginate(param.getPageNo(), param.getPageSize(), select, exceptSelect, param.getP());
        return PageUtil.to(page);
    }
    
    /**
     * 根据userId 和 shopId查用户是否已收藏
     */
    public UserCollect find(Integer userId, Integer shopId){
    	String sql = "select * from user_collect where user_id=? and shop_id=? ";
        return dao.findFirst(sql, userId, shopId);
    }
    
    public boolean delete(Integer userId, Integer shopId){
    	String sql = "delete from user_collect where  user_id=? and shop_id=? ";
    	return (Db.update(sql, userId, shopId) >= 1);
    }
    
}
